package mongo

import (
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
)

func (c *Mongo_Class) InsertOne(db_name, table_name string, data interface{}) string {
	if ret := c.UtilsMongoShell(db_name, table_name, Action_InsertOne, nil, data); ret != nil {
		return ret.(*mongo.InsertOneResult).InsertedID.(primitive.ObjectID).Hex()
	}
	return ""
}

func (c *Mongo_Class) ReplaceOne(db_name, table_name string, filter interface{}, data interface{}) *mongo.UpdateResult {
	if ret := c.UtilsMongoShell(db_name, table_name, Action_ReplaceOne, filter, data); ret != nil {
		return ret.(*mongo.UpdateResult)
	}
	return nil
}

func (c *Mongo_Class) UpdateOne(db_name, table_name string, filter interface{}, data interface{}) *mongo.UpdateResult {
	if ret := c.UtilsMongoShell(db_name, table_name, Action_UpdateOne, filter, data); ret != nil {
		return ret.(*mongo.UpdateResult)
	}
	return nil
}

func (c *Mongo_Class) DeleteOne(db_name, table_name string, filter interface{}, opts ...interface{}) *mongo.DeleteResult {
	if ret := c.UtilsMongoShell(db_name, table_name, Action_DeleteOne, filter, nil, opts...); ret != nil {
		return ret.(*mongo.DeleteResult)
	}
	return nil
}

func (c *Mongo_Class) DeleteMany(db_name, table_name string, filter interface{}) *mongo.DeleteResult {
	if ret := c.UtilsMongoShell(db_name, table_name, Action_DeleteMany, filter, nil); ret != nil {
		return ret.(*mongo.DeleteResult)
	}
	return nil
}

func (c *Mongo_Class) Replace2InsertOne(db_name, table_name string, filter interface{}, data interface{}) string {
	if ret := c.ReplaceOne(db_name, table_name, filter, data); ret != nil {
		if ret.MatchedCount > 0 {
			return ""
		}
	}

	return c.InsertOne(db_name, table_name, data)
}
